package com.ccubee.data.transfer.properties;

import com.ccubee.data.transfer.properties.transfer.DataTransferProperties;
import com.ccubee.data.transfer.types.RejectedExecutionHandlerType;
import lombok.Data;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author ycf
 * @Date 2022/11/15 23:30
 * @Version 1.0
 */
@Data
public class DataProperties {
    public static final String PREFIX = "csap.data";
    /**
     * canal客户端配置
     */
    @NestedConfigurationProperty
    private CanalClientProperties canal;
    /**
     * 数据传输/同步配置
     */
    @NestedConfigurationProperty
    private DataTransferProperties transfer;

    /**
     * 实例的线程池配置
     */
    private Map<String, InstanceThreadPool> instanceThreadPool = Collections.emptyMap();
    /**
     * key is name，数据源配置
     */
    private Map<String, TransferDataSource> dataSource = Collections.emptyMap();

    /**
     * 获取数据源
     *
     * @param dataSourceName 数据源名称
     * @return 数据源结果
     */
    public TransferDataSource dataSource(String dataSourceName) {
        return dataSource.get(dataSourceName);
    }

    /**
     * 获取线程池配置
     *
     * @param name 线程池 名称
     * @return 结果
     */
    public InstanceThreadPool getThreadPool(String name) {
        if (!instanceThreadPool.containsKey(name)) {
            return new InstanceThreadPool();
        }
        return instanceThreadPool.get(name);
    }


    /**
     * 实例的线程池配置
     */
    @Data
    public static class InstanceThreadPool {
        /**
         * 核心线程数
         */
        private int corePoolSize = 10;
        /**
         * 最大线程池数量
         */
        private int maximumPoolSize = 100;
        /**
         * 空闲时常
         */
        private long keepAliveTime = 1L;
        /**
         * 队列数量
         */
        private int blockingQueue = Integer.MAX_VALUE;
        /**
         * 空闲单位
         */
        private TimeUnit unit = TimeUnit.MILLISECONDS;
        /**
         * 驳回类型
         */
        private RejectedExecutionHandlerType rejectedType = RejectedExecutionHandlerType.CallerRunsPolicy;
    }

}
